#from tickee.binding import bind_api
from pytickee.binding import bind_api
import base64
import requests
import simplejson as json

class API(object):
    
    def __init__(self, auth_handler=None, host='api.tickee.org',
                 cache=None, secure=False, api_root='/0',
                 retry_count=0, retry_delay=0, retry_errors=None,
                 parser=None):
        
        self.auth = auth_handler
        self.host = host
        self.api_root = api_root
        
        if secure:
            self.scheme = "https://"
        else:
            self.scheme = "http://"
    
    def authenticate(self, client_key, client_secret, scope='scope_account_mgmt'):
        """
        TODO: Refactor required.
        """
        uri = "http://" + self.host + "/oauth2" + '/token' + ('?grant_type=client_credentials&scope=%s' % scope)
        print uri
        authorization_info = "%s:%s" % (client_key, client_secret)
        headers = {'Authorization': 'Basic %s' % base64.b64encode(authorization_info)}
        r = requests.get(uri, headers=headers)
        print r.content
        j = json.loads(r.content)
        self.access_token = j['access_token']
    
    """ account/show """
    account_show = bind_api(
        path="/account/show",
        require_auth=False,
        allowed_params=['account_id', 'include_events']
    )
    
    """ account/create """
    account_create = bind_api(
        path="/account/create",
        require_auth=True,
        method='POST',
        allowed_params=['email', 'account_name', 'user_id']
    )
    
    """ account/exists """
    account_exists = bind_api(
        path="/account/exists",
        require_auth=True,
        allowed_params=['account_name']
    )
    
    """ events/attendees """
    events_attendees = bind_api(
        path="/events/attendees",
        require_auth=True,
        allowed_params=['event_id', 'eventpart_id']
    )
    
    """ events/create """
    events_create = bind_api(
        path="/events/create",
        require_auth=True,
        method='POST',
        allowed_params=['event_name', 'venue_id', 'units', 'currency']
    )
    
    """ events/show """
    events_show = bind_api(
        path="/events/show",
        require_auth=False,
        allowed_params=['event_id']
    )
    
    """ locations/search """
    locations_search = bind_api(
        path="/locations/search",
        require_auth=True,
        allowed_params=['filter', 'limit']
    )
    
    """ locations/create """
    locations_create = bind_api(
        path="/locations/create",
        require_auth=True,
        method='POST',
        allowed_params=['location_name', 'street_line1', 'street_line2', 'postal_code', 'city', 'country_code']
    )
    
    """ users/create """
    users_create = bind_api(
        path="/users/create",
        require_auth=True,
        method='POST',
        allowed_params=['email', 'password']
    )
    
#    """ users/show """
#    users_show = bind_api(
#        path="/users/show",
#        require_auth=False,
#        allowed_params=['user_id']
#    )
    
    """ users/exists """
    users_exists = bind_api(
        path="/users/exists",
        require_auth=True,
        allowed_params=['email']
    )